/* poporca1.p - INCORPORATE ENTERED ASSAY/GRADE/EXPIRY/STATUS           */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: poporca1.p 81988 2017-12-27 06:50:49Z rgn                         $: */
/* REVISION: 7.0      LAST MODIFIED: 03/11/92   BY: pma *F087*          */
/* REVISION: 7.2      LAST MODIFIED: 10/19/93   BY: tjs *GG44*          */
/* REVISION: 7.3      LAST MODIFIED: 09/11/94   BY: rmh *GM16*          */
/* REVISION: 7.5      LAST MODIFIED: 01/05/95   BY: pma *J040*          */
/* REVISION: 8.6      LAST MODIFIED: 06/19/97   BY: axv *K0F2*          */
/* REVISION: 8.6      LAST MODIFIED: 12/22/97   BY: *J28M* Niranjan R.  */
/* REVISION: 8.6      LAST MODIFIED: 01/19/98   BY: *J2BF* Nirav Parikh */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane */
/* REVISION: 8.6E     LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan   */
/* REVISION: 9.0      LAST MODIFIED: 04/16/99   BY: *J2DG* Reetu Kapoor */
/* REVISION: 9.0      LAST MODIFIED: 05/15/99   BY: *J39K* Sanjeev Assudani */
/* REVISION: 9.0      LAST MODIFIED: 12/24/99   BY: *M0H0* Santosh Rao      */
/* REVISION: 9.1      LAST MODIFIED: 07/20/00   BY: *N0GF* Mudit Mehta      */
/* REVISION: 9.1      LAST MODIFIED: 08/13/00   BY: *N0KQ* myb              */
/* Revision: 1.22.1.1  BY: Paul Donnelly (SB)   DATE: 06/28/03 ECO: *Q00J*  */
/* Old ECO marker removed, but no ECO header exists *F0PN*                  */
/* Revision: 1.23      BY: Shilpa Kamath        DATE: 04/07/06 ECO: *R03R*  */
/* Revision: 1.24      BY: Niranjan Ranka       DATE: 07/05/06 ECO: *R073*  */
/* Revision: 1.25      BY: Niranjan Ranka       DATE: 08/08/06 ECO: *R07W*  */
/* $Revision: 1.27 $   BY: Jean Miller          DATE: 06/15/08  ECO: *R0V6*  */
/*-Revision end---------------------------------------------------------------*/
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

{us/bbi/mfdeclre.i}
{us/bbi/gplabel.i} /* EXTERNAL LABEL INCLUDE */
{us/gp/gpuid.i}
define input parameter pod_recid as recid.

define shared variable msgref as character format "x(20)".

define shared variable site like sr_site no-undo.
define shared variable location like sr_loc no-undo.
define shared variable lotserial like sr_lotser no-undo.
define shared variable lotref like sr_ref no-undo.
define shared variable eff_date like glt_effdate.

define variable chg_assay  like tr_assay initial 0 no-undo.
define variable chg_grade  like tr_grade initial "" no-undo.
define variable chg_expire like tr_expire initial ? no-undo.
define variable chg_status like tr_status initial "" no-undo.
define variable assay_actv as logical no-undo.
define variable grade_actv as logical no-undo.
define variable expire_actv as logical no-undo.
define variable status_actv as logical no-undo.
define variable dir_status like tr_status initial "" no-undo.
define variable dir_ship as logical no-undo.
define variable lc_pt_shelflife as  character no-undo.
define variable l_pt_shelflife as  integer no-undo.
define variable l_POFiscalReceiving as logical no-undo.

{us/px/pxmaint.i}
{us/px/pxphdef.i ppitxr}

for first pt_mstr where pt_mstr.pt_domain = global_domain
   and pt_part = global_part
no-lock: end.

for first pod_det where recid(pod_det) = pod_recid
no-lock: end.

assign
   dir_ship            = no
   dir_status          = pod_rctstat
   l_POFiscalReceiving = no.

/* THE SUBSCRIBE FOR THE STATEMENT BELOW IS AVAILABLE ONLY VIA posmrc.p */
publish "POFiscalReceiving" (output l_POFiscalReceiving).

for first po_mstr where po_mstr.po_domain = global_domain
   and po_nbr  = pod_nbr
no-lock: end.

if po_is_btb then do:

   for first sod_det where sod_det.sod_domain = global_domain
      and sod_nbr  = po_so_nbr
      and sod_line = pod_sod_line
      and sod_compl_stat = ""
   no-lock: end.

   if available sod_det and sod_btb_type = "03" then do:
      dir_ship = yes.

      for first soc_ctrl where soc_ctrl.soc_domain = global_domain
      no-lock: end.

      for first loc_mstr where loc_mstr.loc_domain = global_domain
         and loc_site = pod_site
         and loc_loc = soc_dum_loc
      no-lock: end.

      if available loc_mstr then
         dir_status = loc_status.

   end.

end.

/*INITIALIZE ATTRIBUTE VARIABLES WITH CURRENT SETTINGS*/
if available pod_det then do:
   chg_assay = pod_assay.
   chg_grade = pod_grade.
   chg_expire = pod_expire.
   chg_status = pod_rctstat.
end.

assign
   assay_actv = yes
   grade_actv = yes
   expire_actv = yes
   status_actv = yes.

pause 0.

if (available pod_det and pod_rctstat_active = no)
   or (not available pod_det)
then do:

   for first si_mstr where si_domain = global_domain
      and si_site   = site
   no-lock: end.

   if available pt_mstr and available si_mstr then do:
      {us/px/pxrun.i &PROC  = 'readInventoryDetail' &PROGRAM = 'ppitxr.p'
               &HANDLE=ph_ppitxr
               &PARAM = "(input  pt_mstr.oid_pt_mstr,
                          input  si_mstr.oid_si_mstr,
                          buffer pti_det)"
               &NOAPPERROR = true
               &CATCHERROR = true}
   end.

   if available pti_det and pti_rctpo_active = yes then
      chg_status = pti_rctpo_status.
   else
   if available pt_mstr and pt_rctpo_active = yes then
      chg_status = pt_rctpo_status.
   else do:
      assign
         chg_status  = ""
         status_actv = no.
   end.
end.

if dir_ship then
assign
   chg_status  = dir_status
   status_actv = yes.

/*TEST FOR ATTRIBUTE CONFLICTS*/

msgref = getTermLabel("ITEM_RECEIPT",20).

for first ld_det where ld_det.ld_domain = global_domain
   and ld_site = site
   and ld_loc  = location
   and ld_part = pt_part
   and ld_lot  = lotserial
   and ld_ref  = lotref
no-lock: end.

if available ld_det then do:

   if assay_actv and chg_assay <> ld_assay then return.
   if grade_actv and chg_grade <> ld_grade then return.

   {us/px/pxrun.i &PROC  = 'getFieldDefault' &PROGRAM = 'ppitxr.p'
               &HANDLE=ph_ppitxr
               &PARAM =  "(input  ld_part,
                           input  ld_site,
                           input ""pt_shelflife"",
                           output lc_pt_shelflife)"
               &NOAPPERROR = true
               &CATCHERROR = true}

   l_pt_shelflife = integer (lc_pt_shelflife).

   if expire_actv and
      ((chg_expire <> ? and chg_expire <> ld_expire)
      or (chg_expire = ?
      and available pt_mstr
      and  l_pt_shelflife <> 0
      and eff_date + l_pt_shelflife <> ld_expire)
      or (chg_expire = ?
      and available pt_mstr
      and  l_pt_shelflife = 0
      and ld_expire <> ?))
   then
      return.

   if status_actv and
      (chg_status <> ? and chg_status <> ld_status)
   then
      return.

end.

msgref = "".

if not available ld_det
   and not l_POFiscalReceiving
then do:

   if {us/wh/whgpwhon.i} 
      and lotref = ""
      and can-find(first loc_mstr
          where loc_domain = global_domain
          and   loc_site   = site
          and   loc_loc    = location
          and   loc_perm   = no)
   then do:
      for first sr_wkfl 
         where sr_wkfl.sr_domain = global_domain 
         and   sr_userid         = SessionUniqueID 
         and   sr_site           = site 
         and   sr_loc            = location 
         and   sr_lotser         = lotserial 
      no-lock:
         if not can-find(first ld_det
            where ld_domain = global_domain
            and   ld_site   = sr_site
            and   ld_loc    = sr_loc
            and   ld_part   = pt_part
            and   ld_lot    = sr_lotser
            and   ld_ref    = sr_ref)
         then do:
            create ld_det.
             assign
                ld_domain = global_domain
                ld_site   = site
                ld_loc    = location
                ld_part   = pt_part
                ld_lot    = lotserial
                ld_ref    = sr_ref.
            recno = recid(ld_det).
         end. /* IF NOT CAN-FIND(FIRST b_lddet... */  
      end. /* FOR FIRST sr_wkfl */
   end. /* IF {us/wh/whgpwhon.i} - AIM ACTIVE */
   else do:
      create ld_det.
      assign
         ld_domain = global_domain
         ld_site   = site
         ld_loc    = location
         ld_part   = pt_part
         ld_lot    = lotserial
         ld_ref    = lotref.
      recno = recid(ld_det).
   end. /* ELSE: IF {us/wh/whgpwhon.i} - AIM ACTIVE */

   if available ld_det
   then do:
      if chg_status = "" and status_actv = no then do:

         for first loc_mstr where loc_mstr.loc_domain = global_domain
            and loc_site = site
            and loc_loc  = location
         no-lock: end.

         if available loc_mstr then
         assign
            chg_status = loc_status
            status_actv = yes.

         else do:
  
            for first si_mstr where si_mstr.si_domain = global_domain
               and si_site = site
            no-lock: end.

            if available si_mstr then
            assign
               chg_status = si_status
               status_actv = yes.

         end. /* IF NOT AVAILABLE LOC_MSTR */

      end. /* IF CHG_STATUS = "" AND STATUS_ACTV = NO */

      {us/px/pxrun.i &PROC  = 'getFieldDefault' &PROGRAM = 'ppitxr.p'
               &HANDLE=ph_ppitxr
               &PARAM =  "(input  ld_part,
                           input  ld_site,
                           input ""pt_shelflife"",
                           output lc_pt_shelflife)"
               &NOAPPERROR = true
               &CATCHERROR = true}

      l_pt_shelflife = integer (lc_pt_shelflife).

      if chg_expire = ? and available pt_mstr and l_pt_shelflife <> 0
         then chg_expire = eff_date + l_pt_shelflife.

      if chg_status = ? then
         status_actv = no.

      if assay_actv and chg_assay = ld_assay then assay_actv = no.
      if grade_actv and chg_grade = ld_grade then grade_actv = no.
      if expire_actv and chg_expire = ld_expire then expire_actv = no.
      if status_actv and chg_status = ld_status then status_actv = no.

      /*CHANGE THE ATTRIBUTES*/
      if assay_actv or grade_actv or expire_actv or status_actv then do:
         if assay_actv then ld_assay = chg_assay.
         if grade_actv then ld_grade = chg_grade.
         if expire_actv then ld_expire = chg_expire.
         if status_actv then ld_status = chg_status.
      end.
   end. /* IF AVAILABLE ld_det */
end. /* IF NOT AVAILABLE ld_det AND NOT l_POFiscalReceiving */
